﻿Public Class SelecteerLeveringsbonForm

    Private _idVerkoop As Integer

#Region "Property's"

    Public ReadOnly Property IdVerkoop() As Integer
        Get
            Return _idVerkoop
        End Get
    End Property

    Public ReadOnly Property OmschrijvingVerkoop() As String
        Get
            Return TextBoxOmschrijving.Text
        End Get
    End Property

#End Region

#Region "Constructor/Initialisatie"

    Private Sub SelecteerLeveringsbonForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.KlantenBindingSource.DataSource = Utility.Data
        Me.VerkopenBindingSource.DataSource = Utility.Data
        Me.VerkopenBindingSource.Filter = String.Format("idsoort = {0} and IsFactuurBehandeld = False", Constants.VerkoopsoortIds.FACTUUR)
        Me.VerkopenBindingSource.Sort = "Datum desc, Id desc"
        buttonOk.Enabled = False
        PanelAanvulling.Top = PanelNieuw.Top
        InitGrid()
    End Sub

#End Region

#Region "Events"

    Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
        MyBase.OnKeyUp(e)
        Select Case e.KeyCode
            Case Keys.F12
                Me.DialogResult = DialogResult.Cancel
                Me.Close()
            Case Keys.F2
                If (Not buttonOk.Enabled) Then Return
                Me.DialogResult = DialogResult.OK
                Me.Close()
            Case Keys.F3
                If (Not ButtonLater.Enabled) Then Return
                Me.DialogResult = DialogResult.Ignore
                Me.Close()
            Case Keys.F4
                If (Not ButtonCash.Enabled) Then Return
                Me.DialogResult = DialogResult.Yes
                Me.Close()
        End Select
    End Sub

    Private Sub rbNieuweBon_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbNieuweBon.CheckedChanged
        If (rbNieuweBon.Checked) Then
            _idVerkoop = 0
            VerkopenDataGridView.Visible = False
            buttonOk.Enabled = True
            PanelAanvulling.Visible = False
            PanelNieuw.Visible = True
            TextBoxOmschrijving.Enabled = True
            CancelButton = Me.buttonAnnuleren
        Else
            VerkopenDataGridView.Visible = True
            VerkopenDataGridView.ClearSelection()
            ButtonCash.Enabled = (VerkopenDataGridView.SelectedRows.Count > 0)
            ButtonLater.Enabled = (VerkopenDataGridView.SelectedRows.Count > 0)
            PanelAanvulling.Visible = True
            PanelNieuw.Visible = False
            TextBoxOmschrijving.Enabled = False
            CancelButton = Me.ButtonAnnuleren2
        End If
    End Sub

    Private Sub VerkopenDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles VerkopenDataGridView.SelectionChanged
        If (rbNieuweBon.Checked) Then Return ' kan normaal niet (grid is dan onzichtbaar)
        If VerkopenDataGridView.SelectedRows.Count = 0 Then
            _idVerkoop = 0
            ButtonCash.Enabled = False
            ButtonLater.Enabled = False
            Return
        End If
        Dim gridRow As DataGridViewRow = VerkopenDataGridView.SelectedRows(0)
        Dim datarow As DataRowView = DirectCast(gridRow.DataBoundItem, DataRowView)
        If (datarow Is Nothing) Then
            _idVerkoop = 0
            ButtonCash.Enabled = False
            ButtonLater.Enabled = False
            Return
        End If
        Dim verkopenRow As ArtikelsDataSet.VerkopenRow = DirectCast(datarow.Row, ArtikelsDataSet.VerkopenRow)
        _idVerkoop = verkopenRow.Id
        ButtonCash.Enabled = True
        ButtonLater.Enabled = True
    End Sub

#End Region

#Region "Private helpers"

    Private Sub InitGrid()
        With VerkopenDataGridView
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Verkopen.IdColumn.ColumnName, VerkopenDataGridView)
                .Width = 80
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .HeaderText = "Nr"
                .ToolTipText = "Volgnummer van de leveringsbon"
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Verkopen.IdKlantColumn.ColumnName, VerkopenDataGridView)
                .Width = 200
                .HeaderText = "Klant"
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Verkopen.OmschrijvingColumn.ColumnName, VerkopenDataGridView)
                .Width = 170
                .HeaderText = "Omschrijving"
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Verkopen.DatumColumn.ColumnName, VerkopenDataGridView)
                .Width = 80
            End With
        End With
    End Sub


#End Region

End Class